
#load "unix.cma";;
#load "graphics.cma";;
#load "bplib.cma";;
#load "bebop.cma";;

open Bplib
open Message 
open ListMap
open Inputtools
open Circuit
open OptimiseCds
open Cdsmaker


(** TESTS DIVERS sur l'optimiseur Cds *)

let array_exists fct a =
  let len = Array.length a in
  let rec aux i = 
    if i >= len then false
    else if fct a.(i) then true else aux (succ i)
  in aux 0;;

let count_remove cds_t = 
  let len = Array.length cds_t in
  let count = ref 0 in
  for i = 0 to pred len do
    if cds_t.(i).delete then incr(count) else ()
  done;
  !count
	  
let search_parent cds i = 
  let len = Array.length cds in
  let rec aux accu j =
    if j >= len then List.rev accu 
    else
      let exists (a, b) = a = i in
      if array_exists (List.exists exists) cds.(j).voisins 
      then aux (j::accu) (succ j) else aux accu (succ j)
  in aux [] 0;;

let search_type cds fct = 
  let len = Array.length cds in
  let rec aux accu j =
    if j >= len then List.rev accu 
    else
      if fct cds.(j).noeud_type  
      then aux (j::accu) (succ j) else aux accu (succ j)
  in aux [] 0;;
	
(* Pour se rappeler les types : *)

let c = circuit_final_of_net_list "opt02.bop";;

c#init;;

let cds = c#get_cds;;
cds;;
let cds_opt = OptimiseCds.optimise cds;;
cds_opt;;
c#get_entree;;
c#get_sortie;;

Array.length cds;;

let valeurs_entrees h =
  let t = input_line stdin in
  let g = BoolArray.of_string t in
  Array.blit g 0 h 0 (Array.length h);; 

c#un_cycle valeurs_entrees;;


let coupe cds = Array.init 16 (fun i -> 
			    if i <> 15 
			    then Array.init 10 (fun j -> cds.(10 * i + j))
			    else Array.init 14 (fun j -> cds.(10 * i + j))
			 );; 

let ccoupe = coupe cds;;

ccoupe.(0);;
ccoupe.(1);;
ccoupe.(2);;
ccoupe.(3);;


cds;;
let cds_tag = cds_tague_of_cds cds;;
Array.length cds_tag;;


count_remove cds_tag;;

let coupe0 = coupe cds_tag;; 
coupe0.(3);;
coupe0.(4);;
coupe0.(5);;
coupe0.(6);;
coupe0.(7);;
coupe0.(8);;
coupe0.(9);;
coupe0.(10);;
coupe0.(11);;
coupe0.(12);;
coupe0.(13);;
coupe0.(14);;
coupe0.(15).(14);;

cds_tag.(36);;
cds_tag.(91);;
cds_tag.(94);;

(*
{delete = true;
 infos_node =
  {entree = [|true|]; sortie = [|false|]; voisins = [|[(154, 1); (121, 1)]|];
   noeud_type = Id};
 entrees_taguee = [|Invariante|]}
# 
*)

coupe0.(12).(1);;
coupe0.(15).(4);;

coupe0.(12).(0);;
coupe0.(5).(5);;
coupe0.(9).(1);;
coupe0.(14).(1);;
coupe0.(3).(6);;

(*
  infos_node =
    {entree = [|false|]; sortie = [|false|];
     voisins = [|[(47, 1); (38, 1)]|]; noeud_type = Id};
   entrees_taguee = [|Pointee (36, 0)|]};

;;
      
*)
let les_id = search_type cds (fun t -> t = Id);;
cds.(36);;
cds.(37);;
cds_tag.(36);;
cds_tag.(37);;

cds_tag.(5);;
cds_tag.(38);;
cds_tag.(47);;

search_parent cds 37;;

search_parent cds 123;;
coupe0.(12);;

let regr = search_type cds (function Regr _ -> true | _ -> false);;
(* [51; 53; 55; 57; 59; 92; 95; 98; 100; 102; 104; 106; 139; 142; 145; 147;
   149; 151; 153; 156] *)
cds_tag.(51);;
cds_tag.(53);; 
cds_tag.(149);;

let nott = search_type cds (fun t -> t = Not);;
(* [30; 34; 65; 67; 73; 112; 114] *)
cds_tag.(30);;
cds_tag.(114);;

let s = c#tableprocess.(0);;

Printf.fprintf s.entreepipe "0\n";;
flush s.entreepipe;;


for i = 0 to 5 do
  Printf.fprintf s.entreepipe "1\n";
  flush s.entreepipe;
  print_endline (input_line s.sortiepipe)
done;;


string_of_bits c#get_cds.(1).entree;;

c#get_sortie;;

c#quit;;
